<?php
session_start();

if (!$_SESSION['loggedin']) {
    // User not logged in, redirect to login page
    echo "<script language='javascript'>window.location='index.php'</script>";
}
if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'professor') {
?>
    <html>
        <head>
            <link rel="stylesheet" href="css/style.css" type="text/css" id="" media="print, projection, screen" />
            <script type="text/javascript" src="script/jquery-latest.js"></script>
            <script type="text/javascript" src="script/jquery.tablesorter.js"></script>
            <script type="text/javascript" src="script/jquery.tablesorter.pager.js"></script>
            <script type="text/javascript">
                $(document).ready(function() {
                    // call the tablesorter plugin
                    $("table").tablesorter({
                        // sort on the first column and third column, order asc
                        sortList: [[1,0]],
                        headers: {
                            // assign the secound column (we start counting zero)

                            0: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            2: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            3: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            4: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            5: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            6: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            7: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            8: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            9: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            10: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            11: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            12: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            13: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            14: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            15: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            16: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            17: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            18: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            19: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            20: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            21: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            22: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            23: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            24: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            25: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            26: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            27: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            28: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            29: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            30: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            31: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            32: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            33: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            }
                        }
                        ,
                        widthFixed: true, widgets: ['zebra']
                    })
                    .tablesorterPager({container: $("#pager")});
                    ;
                });

                function confirmLogout()
                {
                    var agree=confirm("Are you sure you want to Logout?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }
                function confirmDrop()
                {
                    var agree=confirm("Are you sure you want to drop selected student?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }

            </script>

            <title>TUP Student Management</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>

        <body style=" height:100%; width: 100%; background-image: url(image/wall6.jpg);">

            <div id="container" style="height:auto; width: 100%;">

                <div style="background-image: url(image/wall12.jpg); height: 110px; width: auto; ">
                </div>

                <div align="center" style="background-image: url(image/wall11.jpg);  height: 74px; width: auto;">


                    <div align="center" style="background-image: url(image/wall11.jpg);  height: 74px; width: auto;">

                        <div align="center" style=" height: 4px; width: auto;"></div>
                        <div align="center" style=" height: 22px; width: auto;">
                            <a href="AMMSelectSection.php"><b>[ Select Section ]</b></a>&nbsp;&nbsp;
                        </div>
                        <div align="center" style=" height: 4px; width: auto;"></div>
                        <div align="center" style=" height: 22px; width: auto;">
                            <a href="AMMRecordAttendance.php">Add Attendance</a>&nbsp;&nbsp;
                            <a href="AMMViewAttendance.php">[ View Attendance ]</a>&nbsp;&nbsp;

                        </div>

                        <div  align="left" style="padding-left: 4%; height: 18px; width: 46%;float:left;"><?php echo "<aa class=\"smallw\"><b>Welcome " . $_SESSION['fname'] . ", Last login: " . $_SESSION['lastlogin'] . "</b></aa>"; ?>
                        </div>
                        <div align="right" style="padding-right: 4%; height: 18px; width: 46%;float:left;"><bb>|</bb>
                            <a class="small" href="ProfEditProfile.php"><b>Edit Profile</b></a>
                            <bb>|</bb>
                            <a onclick="return confirmLogout()" class="small" href="Logout.php"><b>Logout</b></a>
                            <bb>|</bb></div>

                    </div>
                </div>

                <div align="center" style="  float:left; height:auto; width: 100%;">

                <?php
                if ($_SESSION['codesecsemyear'] == null) {
                    $codesecsemyear = $_POST['codesecsemyear'];
                    $_SESSION['codesecsemyear'] = $codesecsemyear;
                }
                else
                    $codesecsemyear= $_SESSION['codesecsemyear'];
                //$codesecsemyear = "ABCDEFGS121st Term2012";
                include 'DBLibrary/config.php';
                include 'DBLibrary/openDB.php';

                mysql_select_db($dbname);
                $querytext = "select * from coursetable where codesecsemyear='" . $codesecsemyear . "'";
                $resulttext = mysql_query($querytext);
                $text = mysql_fetch_array($resulttext);

                $nummeet = $text['num_of_meeting'];
                ?><h2>Students in <?php echo $text['coursecode'] . " " . $text['section'] . " " . $text['semester'] . " SY:" . $text['year']; ?></h2>

                <form  method="post" action="attendancelist.php">
                    <input type="text" name="codesecsemyear" size="1" value="<?php echo $codesecsemyear; ?>" style="visibility:hidden;"/>
                    <br><input name="Export" class="submit" type="submit" value="Export"/>

                </form>
                <?php
//SELECT * FROM enrollment e left join student s on e.studidno=s.studidno;
                include 'DBLibrary/config.php';
                include 'DBLibrary/openDB.php';
                mysql_select_db($dbname);
                $querystud = "SELECT * FROM enrollment e left join student s on e.studidno=s.studidno where codesecsemyear='" . $codesecsemyear . "' order by s.lname, s.fname";
                $allstud = mysql_query($querystud);
                $ctr = 0;


                $numrows = mysql_num_rows($allstud);

                echo "<aa>" . $numrows . " total results</aa>";
                echo "<aa><br> number of meetings " . $nummeet . "</aa><br>";
                ////////////////////////////////////

                mysql_select_db($dbname);
                $queryattend = "select * from attendance where codesecsemyear='" . $codesecsemyear . "'";
                $resultattend = mysql_query($queryattend);
                //////number of entries [][][][][]
                $attendnum = mysql_num_rows($resultattend);

                for ($y = 0; $y < $attendnum; $y++) {
                    mysql_select_db($dbname);
                    $met = $y + 1;
                    $queryattend = "select * from attendance where codesecsemyear='" . $codesecsemyear . "' and meeting_num='" . $met . "'";
                    $resultattend = mysql_query($queryattend);
                    $attend = mysql_fetch_array($resultattend);
                    $status = $attend['status'];
                    $statusarray = explode(";", $status);
                    for ($x = 0; $x < $numrows; $x++) {
                        $arratend[$x][$y] = $statusarray[$x];

                        //echo $statusarray[$x];
                    }
                    //echo "<br>";
                }

                //print_r($arratend);
                ?>
                <table  border="0" id="myTable" class="tablesorter" style="width: 95%;" align="center">
                    <thead><tr>
                            <th style="width: 2.5%;">No</th>

                            <th style="width: 17%;">Name</th>
                            <?php
                            for ($ct = 1; $nummeet >= $ct; $ct++) {
                                $querytitle = "select * from attendance where codesecsemyear='" . $codesecsemyear . "' and meeting_num='" . $ct . "'";
                                $resulttitle = mysql_query($querytitle);
                                $title = mysql_fetch_array($resulttitle);
                                echo "<th title=\"" . $title['date'] . "\" style=\"width: 2.25%;\">" . $ct . "</th>";
                            }
                            ?>

                            <th style="width: 2.5%; color: #e90202;">A</th>
                            <th style="width: 2.5%; color: #3232CD;">L</th>
                            <th style="width: 2.5%; color: #32CD32;">P</th>

                        </tr></thead>
                    <tfoot><tr>
                            <th>No</th>
                            <th>Name</th>
                            <?php
                            for ($ct = 1; $nummeet >= $ct; $ct++) {
                                $querydate = "select * from attendance where codesecsemyear='" . $codesecsemyear . "' and meeting_num='" . $ct . "'";
                                $resultdate = mysql_query($querydate);
                                $datetitle = mysql_fetch_array($resultdate);
                                echo "<th title=\"aaaaaaaaaa\">" . $ct . "</th>";
                            }
                            ?>
                            <th>A</th>
                            <th>L</th>
                            <th>P</th>

                        </tr></tfoot>
                    <tbody>

                        <?php
                            //$row['lname'] . ",&nbsp;" . $row['fname'] . "&nbsp;" . $row['mname']
                            while ($row = mysql_fetch_array($allstud)) {
                                $TP = 0;
                                $TA = 0;
                                $TL = 0;
                                $ctrnum = $ctr + 1;
                                //if()
                                //echo "<tr>" . "<td>" . $ctr . "</td>";<th>No.</th>
                                echo "<tr><td  align=\"center\">" . $ctrnum . "</td>";
                                echo "<td>" . $row['lname'] . ",&nbsp;" . $row['fname'] . "&nbsp;" . $row['mname'] . "</td>";

                                for ($x = 0; $x < $attendnum; $x++) {
                                    // echo "[" . $ctr . "]" . "[" . $x . "]||||";style="width: 2.5%; color: #e90202;"
                                    if ($arratend[$ctr][$x] == 'A') {
                                        echo "<td style=\"color: #e90202;\"  align=\"center\">" . $arratend[$ctr][$x] . "</td>";
                                        $TA++;
                                    } else if ($arratend[$ctr][$x] == 'L') {
                                        echo "<td  style=\"color: #3232CD; font-style: bold;\"  align=\"center\">" . $arratend[$ctr][$x] . "</td>";
                                        $TL++;
                                    } else if ($arratend[$ctr][$x] == 'P') {
                                        echo "<td style=\"color: #32CD32;\"  align=\"center\">" . $arratend[$ctr][$x] . "</td>";
                                        $TP++;
                                    }else
                                        echo "<td style=\"color: #32CD32;\"  align=\"center\"> </td>";
                                }

                                for ($x = 0; $x < $nummeet - $attendnum; $x++)
                                    echo "<td  align=\"center\"></td>";
                                //echo "<br>";

                                echo "<td  align=\"center\">" . $TA . "</td>";
                                echo "<td  align=\"center\">" . $TL . "</td>";
                                echo "<td  align=\"center\">" . $TP . "</td>";
                                echo "</tr>";
                                $ctr++;
                            }
                            include 'DBLibrary/closeDB.php';
                        ?> </tbody>
                    </table>

                    <div id="pager" class="pager" align="center" style="float:left; height: auto; width: 100%;">
                        <form>
                            <img src="addons/pager/icons/first.png" class="first"/>
                            <img src="addons/pager/icons/prev.png" class="prev"/>
                            <input readonly="readonly" type="text" class="pagedisplay"/>
                            <img src="addons/pager/icons/next.png" class="next"/>
                            <img src="addons/pager/icons/last.png" class="last"/>
                            <select class="pagesize">
                                <option selected="selected"  value="10">10</option>

                                <option value="20">20</option>
                                <option value="30">30</option>
                                <option  value="40">40</option>
                            </select>
                        </form>
                        <div id="footer" style="background-image: url(image/wall11.jpg);clear:both;text-align:center; height: 26px; width:auto;">
                            <div align="center" style=" height: 4px; width: auto;"></div>
                            <bb class ="small">Copyright &#169; 2011</bb>
                        </div>
                    </div>
                    <br>


                </div>
            </div>

        </body>
    </html>
<?php
                        } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'admin') {
                            echo "<script language='javascript'>window.location='UMMViewUser.php'</script>";
                        } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'registrar') {
                            echo "<script language='javascript'>window.location='SMMViewStud.php'</script>";
                        }
?>